package com.vegetable.modules.controller.finance;

import com.vegetable.common.annotation.SysLog;
import com.vegetable.common.utils.R;
import com.vegetable.modules.controller.AbstractController;
import com.vegetable.modules.entity.finance.dto.PaymentApprovalDTO;
import com.vegetable.modules.service.finance.PaymentApprovalService;
import com.vegetable.modules.service.finance.PaymentProcessService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 付款审批控制器
 *
 * @author Qoder
 */
@Slf4j
@RestController
@RequestMapping("/payment/approval")
@Api(value = "付款审批", tags = "付款审批")
public class PaymentApprovalController extends AbstractController {

    @Autowired
    private PaymentApprovalService paymentApprovalService;
    
    @Autowired
    private PaymentProcessService paymentProcessService;

    /**
     * 提交付款审批
     *
     * @param paymentId 付款ID
     * @param payeeName 收款人全称（必填）
     * @param payeeBankAccount 收款人银行账号（必填）
     * @param payeeBankName 收款人开户行（必填）
     * @return 结果
     */
    @SysLog(value = "提交付款审批")
    @PostMapping("/submit")
    @ApiOperation(value = "提交付款审批", notes = "提交指定付款记录的企业微信审批。收款人信息为必填项，系统会验证银行账号是否在客户银行账号列表中。")
    public R submitPaymentApproval(@RequestParam Long paymentId,
                                   @RequestParam String payeeName,
                                   @RequestParam String payeeBankAccount,
                                   @RequestParam String payeeBankName) {
        log.info("开始提交付款审批，付款ID: {}, 收款人: {}, 银行账号: {}, 开户行: {}", 
                paymentId, payeeName, payeeBankAccount, payeeBankName);
        
        // 验证参数
        if (payeeName == null || payeeName.isEmpty()) {
            return R.fail("收款人全称不能为空");
        }
        
        if (payeeBankAccount == null || payeeBankAccount.isEmpty()) {
            return R.fail("收款人银行账号不能为空");
        }
        
        if (payeeBankName == null || payeeBankName.isEmpty()) {
            return R.fail("收款人开户行不能为空");
        }
        
        // 创建PaymentApprovalDTO对象
        PaymentApprovalDTO approvalDTO = new PaymentApprovalDTO();
        approvalDTO.setPaymentId(paymentId);
        approvalDTO.setPayeeName(payeeName);
        approvalDTO.setPayeeBankAccount(payeeBankAccount);
        approvalDTO.setPayeeBankName(payeeBankName);
        
        return paymentApprovalService.submitPaymentApproval(paymentId, approvalDTO);
    }

    /**
     * 处理企业微信审批结果
     */
    @SysLog(value = "处理企业微信审批结果")
    @PostMapping("/handleResult")
    @ApiOperation(value = "处理企业微信审批结果", notes = "处理企业微信审批结果并更新付款状态")
    public R handleWxApprovalResult(@RequestBody PaymentApprovalDTO approvalDTO) {
        log.info("开始处理企业微信审批结果: {}", approvalDTO);
        return paymentApprovalService.handleWxApprovalResult(approvalDTO);
    }
    
    /**
     * 处理审批结果并执行支付
     */
    @SysLog(value = "处理审批结果并执行支付")
    @PostMapping("/process/{paymentId}")
    @ApiOperation(value = "处理审批结果并执行支付", notes = "处理审批结果并在审批通过后执行支付")
    public R processApprovalAndPayment(@PathVariable Long paymentId, 
                                       @RequestParam boolean isApproved, 
                                       @RequestParam(required = false, defaultValue = "") String remark) {
        log.info("开始处理审批结果并执行支付，付款ID: {}, 审批结果: {}, 备注: {}", paymentId, isApproved, remark);
        return paymentProcessService.processApprovalAndPayment(paymentId, isApproved, remark);
    }
    
    /**
     * 手动拉取付款单的审批情况
     *
     * @param paymentId 付款ID
     * @return 审批情况结果
     */
    @SysLog(value = "手动拉取付款单审批情况")
    @GetMapping("/pullApprovalStatus/{paymentId}")
    @ApiOperation(value = "手动拉取付款单审批情况", notes = "手动从企业微信拉取指定付款单的最新审批情况并更新系统状态")
    public R pullPaymentApprovalStatus(@PathVariable Long paymentId) {
        log.info("开始手动拉取付款单审批情况，付款ID: {}", paymentId);
        return paymentApprovalService.pullPaymentApprovalStatus(paymentId);
    }
}